﻿function AzureStorageAccount() {

    var self = this;
    
    var accName = '';
    var accKey = '';
    var devStorage = false;
    var https = false;

    var tablesUrl = '';
    var blobsUrl = '';
    var queuesUrl = '';
    
    var base64 = new Base64();
    var utf8 = new UTF8();

    var hmac;
    
    function getTablesUrl() {
        return tablesUrl;
    }
    
    function getBlobsUrl() {
        return blobsUrl;
    }
    
    function getQueuesUrl() {
        return queuesUrl;
    }
    
    function setAccountKey(accountKey) {
        accKey = accountKey;
        var accountKeyBin = base64.decode(accKey);
        hmac = new HmacSha256(accountKeyBin);
    }
    
    function setAccountNameAndKey(accountName, accountKey) {
        accName = accountName;
        setAccountKey(accountKey);
        devStorage = false;
        
        tablesUrl = 'http://' + accountName + '.table.core.windows.net/';
        blobsUrl = 'http://' + accountName + '.blob.core.windows.net/';
        queuesUrl = 'http://' + accountName + '.queue.core.windows.net/';
    }
    
    function useDevStorage() {
        accName = 'devstoreaccount1';
        setAccountKey('Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==');
        devStorage = true;
        
        tablesUrl = 'http://127.0.0.1:10002/' + accName + '/';
        blobsUrl = 'http://127.0.0.1:10000/' + accName + '/';
        queuesUrl = 'http://127.0.0.1:10001/' + accName + '/';
    }
    
    function getAccountName() {
        return accName;
    }

    function sign(str) {
        var stringToSignBytes = utf8.getBytes(str);
        var hmacHash = hmac.encode(stringToSignBytes);
        return base64.encode(hmacHash);
    }
    
    self.setAccountNameAndKey = setAccountNameAndKey;
    self.useDevStorage = useDevStorage;
    self.getAccountName = getAccountName;
    self.sign = sign;
    
    self.getTablesUrl = getTablesUrl;
    self.getBlobsUrl = getBlobsUrl;
    self.getQueuesUrl = getQueuesUrl;

}